/*trigger RestrictUpdateAttachmentName on Attachment (before update) {
    if(Trigger.isUpdate){
        for(Attachment a: Trigger.new){
        	CallidusDataSource__c[] cdsList = [select In_Use_Attachments__c from CallidusDataSource__c where id = :a.parentId];	
            if(cdsList.size() > 0)
            {
            	if(cdsList[0].In_Use_Attachments__c != null && cdsList[0].In_Use_Attachments__c.indexOf(a.Id) != -1)
                    a.addError('Attachment name cannot be changed.');
            }
        	
        }
    }
}
*/

trigger RestrictUpdateAttachmentName on Attachment (before update) {
    if(Trigger.isUpdate)
    {
    	Map<ID, Attachment> attachmentParentIdMap = new Map<ID, Attachment>{};
        for(Attachment a: Trigger.new)
        {
        	attachmentParentIdMap.put(a.parentId, a);
        }
        if(attachmentParentIdMap.size() > 0)
        {    	
        	CallidusDataSource__c[] cdsList = [select In_Use_Attachments__c from CallidusDataSource__c where id in :attachmentParentIdMap.keyset()];	
            if(cdsList.size() > 0)
            {
            	for(CallidusDataSource__c cd: cdsList)
            	{
	            	for(ID idVal: attachmentParentIdMap.keyset())
	            	{
	            		Attachment att = attachmentParentIdMap.get(idVal);
	            		if(cd.In_Use_Attachments__c != null && cd.In_Use_Attachments__c.indexOf(att.id) != -1)
	            		{
	                    	att.addError('Attachment name cannot be changed.');
	                    	break;
	            		}
	            	}
            	}
            }
            
            Template__c[] tList = [select id, name, createdbyid from Template__c where id in :attachmentParentIdMap.keyset()];
            if(tList.size() > 0)
            {
            	Schema.Describesobjectresult tSobject = Template__c.getSObjectType().getDescribe();
		    	Map<ID,User> uMap = new Map<ID, User>{};
		    	Map<ID,Template__c> tMap = new Map<ID, Template__c>{};
		    	ID[] uList = new ID[]{};
            	
		    	for(Template__c t: tList)
		    	{
		    		uList.add(t.createdById);
		    		tMap.put(t.id, t);
		    	}
	    	
		    	User[] users = [select id, firstname, email from User where id in :uList];
		    	for(User u: users)
		    	{
		    		uMap.put(u.id, u);
		    	}
		        Messaging.SingleEmailMessage[] mailList = new Messaging.SingleEmailMessage[]{};                                              
		        Template__c[] errorTemplateList = new Template__c[]{};                                           
            	
            	if(!tSobject.isAccessible() || !tSobject.isCreateable() || !tSobject.isUpdateable() || !tSobject.isDeletable())
            	{
	            	for(ID idVal: attachmentParentIdMap.keyset())
	            	{
				      	Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
			            String actingUserName = UserInfo.getName();
			            User u = uMap.get((attachmentParentIdMap.get(idVal)).createdById);
			            String[] toAddresses = new String[] {u.Email}; 
			          	mail.setToAddresses(toAddresses);
			            mail.setSubject(actingUserName+' tried to update the Template '+tMap.get(idVal).Name);
			            mail.setBccSender(false);
			            mail.setUseSignature(false);
			            mail.setPlainTextBody(actingUserName+' tried to update the Template '+tMap.get(idVal).Name);
			            mail.setHtmlBody('Dear <b>'+u.FirstName+'</b>,<br/><br/>'+actingUserName+' tried to perform CRUD operation on the Template '+tMap.get(idVal).Name+'.<br/><br/>Thanks');
			            mailList.add(mail);
		    		
	                   	errorTemplateList.add(tMap.get(idVal));
	            	}
            		
            	}
		       	Messaging.sendEmail(mailList);
		       	for(Template__c t: errorTemplateList)
		       	{
					t.addError('User has insufficient privileges on Template object');			       		
		       	}
		       	
            }            
        }
        	
        
    }
}